package nGui.controls;

import nGui.controls.Control;

class List extends Control
{
	var length : Int;
	var _dropdown : Bool;
	var _editbox : Bool;
	var _readonly : Bool;
	var _multiple : Bool;
	var _showdropdown : Bool;
	var _numlines : Int;
	public var onChange(default,change) : Void -> Void;
	public var selectedItems(getSelectedItems,setSelectedItems) : Dynamic;
	public var dropdown(getDropdown,setDropdown) : Bool;
	public var editbox(getEditbox,setEditbox) : Bool;
	public var readonly(getReadonly,setReadonly) : Bool;
	public var multiple(getMultiple,setMultiple) : Bool;
	public var showdropdown(getShowdropdown,setShowdropdown) : Bool;
	public var numlines(getNumlines,setNumlines) : Int;

	public function new()
	{
		super();
		length = -1;
		var val : String = "LIST_CB";
		__c = ngui_list( untyped val.__s );
	}

	private function change( func : Void -> Void )
	{
		Reflect.setField(this,"onChange",func);
		this.setCallback( "ACTION", onChange, null );
		return func;
	}
	
	public function getSelectedItems() : Dynamic
	{
		var v : String = value;
		if ( _multiple )
		{
			var a : Array<Bool> = new Array();
			for ( i in 0...v.length )
				a.push( ( v.charAt( i ) == "+" ) );
			return a;
		}
		else if ( ! _editbox )
			return Std.parseInt( v ) - 1;
		else return v;
	}
	
	public function setSelectedItems( val : Dynamic )
	{
		if ( _multiple && Std.is( val, Array ) )
		{
			var s : String = "";
			for ( i in 0...val.length )
				s += if ( val[i] ) "+" else "-";
			value = s;
		}
		else if ( Std.is( val, Int ) )
			value = val + 1;
		else
			value = val;
	}

	public function appendItem( val : String )
	{
		this.setAttribute( Std.string(++length + 1), val );
		return length;
	}

	function setDropdown( val : Bool )
	{
		_dropdown = val;
		var isDD = if ( val ) "YES" else "NO";
		this.setAttribute( "DROPDOWN", isDD );
		return null;
	}

	function getDropdown() : Bool
	{
		return _dropdown;
	}


	function setEditbox( val : Bool )
	{
		_editbox = val;
		var isDD = if ( val ) "YES" else "NO";
		this.setAttribute( "EDITBOX", isDD );
		return null;
	}

	function getEditbox() : Bool
	{
		return _editbox;
	}


	function setReadonly( val : Bool )
	{
		_readonly = val;
		var isDD = if ( val ) "YES" else "NO";
		this.setAttribute( "READONLY", isDD );
		return null;
	}

	function getReadonly() : Bool
	{
		return _readonly;
	}


	function setShowdropdown( val : Bool )
	{
		_showdropdown = val;
		var isDD = if ( val ) "YES" else "NO";
		this.setAttribute( "SHOWDROPDOWN", isDD );
		return null;
	}

	function getShowdropdown() : Bool
	{
		return _showdropdown;
	}


	function setMultiple( val : Bool )
	{
		_multiple = val;
		var isDD = if ( val ) "YES" else "NO";
		this.setAttribute( "MULTIPLE", isDD );
		return null;
	}

	function getMultiple() : Bool
	{
		return _multiple;
	}


	function setNumlines( val : Int )
	{
		this.setAttribute( "VISIBLE_ITEMS", Std.string( val ) );
		return null;
	}

	function getNumlines() : Int
	{
		return Std.parseInt( this.getAttribute( "VISIBLE_ITEMS" ) );
	}


	static var ngui_list = neko.Lib.load("nGui","ngui_element_list",1);
}
